package com.example.front.controller;

import com.example.front.service.IShippingService;
import com.example.front.service.UserService;
import com.example.gok_mall.entity.Shipping;
import com.example.gok_mall.entity.User;
import com.example.gok_mall.reponseData.ResultData;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 *收货地址接口，曾 删 改 查  一个列表页
 */
@Controller
@ResponseBody
@RequestMapping("/shipping")
public class ShippingController {

    @Autowired
    private IShippingService iShippingService;
    @Autowired
    private UserService userService;

    //添加地址接口
    @PostMapping("/add.do")
    public ResultData add(HttpSession session, Shipping shipping){
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return iShippingService.add(user.getId(), shipping);
        } else {
            return ResultData.fail("添加地址失败");
        }
    }

    //删除地址接口
    @DeleteMapping("/del.do")
    public ResultData del(HttpSession session, Integer shippingId){
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
        return iShippingService.del(user.getId(), shippingId);
        } else {
            return ResultData.fail("删除地址失败");
        }
    }

    //修改地址接口
    @PostMapping("/update.do")
    public ResultData update(HttpSession session, Shipping shipping){
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
        return iShippingService.update(user.getId(), shipping);
        } else {
            return ResultData.fail("修改地址失败");
        }
    }


    //查询地址接口
    @GetMapping("/select.do")
    public ResultData<Shipping> select(HttpSession session, Integer shippingId){
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
        return iShippingService.select(user.getId(), shippingId);
        } else {
            return ResultData.fail("查询地址失败");
        }
    }

    //查询所有地址接口
    @GetMapping("/list.do")
    public ResultData<PageInfo> list(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize, HttpSession session){
        User user = (User) session.getAttribute("userInfo");
        if (user == null) {
            return ResultData.fail("用户未登录，请登录后再试");
        }
        if (userService.checkAdminRole(user) != null) {
            return iShippingService.list(user.getId(), pageNum, pageSize);
        }else {
                return ResultData.fail("查询所有的收货地址失败");
            }
        }
    }
